import time
import random
from typing import List
import os

from magic_google import MagicGoogle

ENABLE_PROXY = True  # 是否启用代理服务器，记得你自己改成False


def get_proxies():
    """
    获取代理服务器
    :return:
    """
    return [{
        'http': 'http://127.0.0.1:1081',
        'https': 'http://127.0.0.1:1081',
    }] if ENABLE_PROXY else None


PROXIES = get_proxies()

# Or MagicGoogle()
mg = MagicGoogle(PROXIES)


def read_companies(path='companies.txt'):
    with open(path, encoding='utf-8') as f:
        return [x.strip() for x in f.readlines()]


def search_tax_director(companies: List[str], keyword='tax director', first_n=3):
    top_n_tax_directors_in_companies = []
    for comp in companies:
        top_n_tax_directors = []
        cnt = 0
        for url in mg.search_url(query=comp + " " + keyword):
            top_n_tax_directors.append(url)
            cnt += 1
            if cnt >= 3:
                break
        top_n_tax_directors_in_companies.append(top_n_tax_directors)
        write_one(comp, top_n_tax_directors)
        sleep_time_in_sec = random.randint(5, 30)
        # print("{} done. Next will start in {}s. Result: {}".format(comp, sleep_time_in_sec,top_n_tax_directors))
        time.sleep(sleep_time_in_sec)


def write_one(company, urls):
    with open(os.path.join("linkedin", 'data', company+'.txt')) as f:
        f.writelines(urls)


def main():
    companies = read_companies()
    search_tax_director(companies, keyword='tax director', first_n=3)


if __name__ == '__main__':
    main()